﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHISInteroperability.Authentication
{
    public class AuthorizationAgent
    {
        private string _Username;
        private string _Password;
        private string _AuthorizationText;

        public AuthorizationAgent(string username, string password)
        {
            this._Username = username;
            this._Password = password;
            this._AuthorizationText = GetAuthorizationText();
        }

        public string AuthorizationText
        {
            get { return this._AuthorizationText; }
        }

        public string Username
        {
            get { return this._Username; }
        }

        public string Password
        {
            get { return this._Password; }
        }

        public void ChangeLoginCredentials(string username, string password)
        {
            this._Username = username;
            this._Password = password;
            this._AuthorizationText = GetAuthorizationText();
        }

        private string GetAuthorizationText()
        {
            string credentials = string.Format("{0}:{1}", _Username, _Password);
            char[] credentialsArray = credentials.ToCharArray();
            byte[] authenticationBytes = Encoding.UTF8.GetBytes(credentialsArray);

            string encodedCredentials = Convert.ToBase64String(authenticationBytes);
            string output = string.Format("Basic {0}", encodedCredentials);
            return output;
        }
    }
}
